package com.xincongjun.media.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.concurrent.ThreadLocalRandom;

public class DateTimeUtil {

    public static final Logger logger = LoggerFactory.getLogger(DateTimeUtil.class);
    public static final String EXIF_TIME_PATTERN = "yyyy:MM:dd HH:mm:ss";
    public static final String STANDARD_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public static final String FILE_RENAME_TIME_PATTERN = "yyyy-MM-dd_HH-mm-ss";
    public static final String ZONE_UTC = String.valueOf(ZoneOffset.UTC);
    public static final String ZONE_LOCAL = String.valueOf(ZoneId.systemDefault());

    //    inputTime 时间格式为 "yyyy-MM-dd HH:mm:ss"
    public static String convertToTargetTimeZone(String inputTime, String sourceTimeZone, String targetTimeZone, String dateTimeFormat) {
        ZonedDateTime sourceZonedDateTime = parseToZonedDateTime(inputTime, ZoneId.of(sourceTimeZone), dateTimeFormat);

        ZonedDateTime targetZonedDateTime = convertTimeZone(sourceZonedDateTime, ZoneId.of(targetTimeZone));

        return formatZonedDateTimeToString(targetZonedDateTime, ZoneId.of(targetTimeZone), dateTimeFormat);
    }

    private static ZonedDateTime parseToZonedDateTime(String inputTime, ZoneId zone, String dateTimeFormat) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateTimeFormat);
        LocalDateTime localDateTime = LocalDateTime.parse(inputTime, formatter);
        return localDateTime.atZone(zone);
    }

    private static ZonedDateTime convertTimeZone(ZonedDateTime zonedDateTime, ZoneId targetZone) {
        return zonedDateTime.withZoneSameInstant(targetZone);
    }

    private static String formatZonedDateTimeToString(ZonedDateTime zonedDateTime, ZoneId zone, String dateTimeFormat) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateTimeFormat);
        return zonedDateTime.withZoneSameInstant(zone).format(formatter);
    }
    //    inputTime 时间格式为 "yyyy-MM-dd HH:mm:ss"
    public static String convertDateFormat(String inputTime, String inputFormat, String outputFormat) {
        DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern(inputFormat);
        DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern(outputFormat);

        LocalDateTime localDateTime = LocalDateTime.parse(inputTime, inputFormatter);

        return localDateTime.format(outputFormatter);
    }

    //    inputTime 时间格式为 "yyyy-MM-dd HH:mm:ss"
    public static String toUtcIso8601(String inputTime, String inputFormat) {
        DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern(inputFormat);
        LocalDateTime localDateTime = LocalDateTime.parse(inputTime, inputFormatter);

        Instant utcInstant = localDateTime.toInstant(ZoneOffset.ofHours(8));

        return utcInstant.toString();
    }

    //    startTimeStr，endTimeStr 时间格式为 "yyyy-MM-dd HH:mm:ss"
    public static String generateRandomTimeString(String startTimeStr, String endTimeStr) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        LocalDateTime startTime = LocalDateTime.parse(startTimeStr, formatter);
        LocalDateTime endTime = LocalDateTime.parse(endTimeStr, formatter);

        if (startTime.equals(endTime)) {
            return startTime.format(formatter);
        }

        long startMillis = startTime.toInstant(ZoneOffset.UTC).toEpochMilli();
        long endMillis = endTime.toInstant(ZoneOffset.UTC).toEpochMilli();

        long randomMillis = ThreadLocalRandom.current().nextLong(startMillis, endMillis);

        return Instant.ofEpochMilli(randomMillis)
                .atZone(ZoneOffset.UTC)
                .toLocalDateTime()
                .format(formatter);
    }

    public static String formatCompactDatetime(String datetimeStr) {
        try {
            SimpleDateFormat inputFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = inputFormat.parse(datetimeStr);
            return outputFormat.format(date);
        } catch (Exception e) {
            throw new IllegalArgumentException("输入的日期格式有误：" + datetimeStr, e);
        }
    }

    public static String formatTimestampMillis(String timestampStr) {
        long timestamp = Long.parseLong(timestampStr);
        Date date = new Date(timestamp);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

}
